package com.example.springbootrbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springbootrbac.entity.*;
import com.example.springbootrbac.repository.*;
import com.example.springbootrbac.service.DeleteService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Service
public class DeleteServiceImpl implements DeleteService {
    @Resource(name = "announcementRepository")
    AnnouncementRepository announcementRepository;
    @Resource(name = "roomRepository")
    RoomRepository roomRepository;
    @Resource(name = "courseRepository")
    CourseRepository courseRepository;
    @Resource(name = "messagesBoardRepository")
    MessagesBoardRepository messagesBoardRepository;
    @Resource(name = "userRepository")
    UserRepository userRepository;

    @Override
    public String deleteAnnouncement(long id, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserEntity user = (UserEntity) session.getAttribute("user");
        AnnouncementEntity announcement = announcementRepository.selectOne(
                new QueryWrapper<AnnouncementEntity>()
                        .eq("id", id)
        );
        if (ObjectUtils.isEmpty(announcement)) {
            return null;
        }
        if (!"DBA".equals(user.getIdentity())) {                //是否为管理员
            System.out.println("不是管理员，不能删除公告");
            return null;
        } else {
            QueryWrapper<AnnouncementEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", id);
            int del = announcementRepository.delete(queryWrapper);
            if (!ObjectUtils.isEmpty(del)) {
                return "token:" + del;
            }
        }


        return null;
    }

    @Override
    public String deleteRoom(long id, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserEntity user = (UserEntity) session.getAttribute("user");
        RoomEntity room = roomRepository.selectOne(
                new QueryWrapper<RoomEntity>()
                        .eq("id", id)
        );
        if (ObjectUtils.isEmpty(room)) {
            return null;
        }
        if (!user.getUsername().equals(room.getCreateBy())) {

            if (!"DBA".equals(user.getIdentity())) {                //是否为管理员
                return null;
            } else {
                QueryWrapper<RoomEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("id", id);
                int del = roomRepository.delete(queryWrapper);
                if (!ObjectUtils.isEmpty(del)) {
                    return "token:" + del;
                }
            }

        }
        QueryWrapper<RoomEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int del = roomRepository.delete(queryWrapper);
        if (!ObjectUtils.isEmpty(del)) {
            return "token:" + del;
        }
        return null;
    }

    @Override
    public String deleteCourse(long id, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserEntity user = (UserEntity) session.getAttribute("user");
        CourseEntity course = courseRepository.selectOne(
                new QueryWrapper<CourseEntity>()
                        .eq("id", id)
        );
        if (ObjectUtils.isEmpty(course)) {
            return null;
        }
        if (!user.getUsername().equals(course.getCreateBy())) {
            if (!"DBA".equals(user.getIdentity())) {                //是否为管理员
                return null;
            } else {
                QueryWrapper<CourseEntity> queryWrapper = new QueryWrapper<>(); //是否为本人操作
                queryWrapper.eq("id", id);
                int del = courseRepository.delete(queryWrapper);
                if (!ObjectUtils.isEmpty(del)) {
                    return "token:" + del;
                }
            }

        }
        QueryWrapper<CourseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int del = courseRepository.delete(queryWrapper);
        if (!ObjectUtils.isEmpty(del)) {
            return "token:" + del;
        }
        return null;
    }

    @Override
    public String deleteUser(long id, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserEntity user = (UserEntity) session.getAttribute("user");

        if ("DBA".equals(user.getIdentity())){
            QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", id);
            int del = userRepository.delete(queryWrapper);
            if (!ObjectUtils.isEmpty(del)) {
                return "token:" + del;
            }
        }
        return null;
    }

    @Override
    public String deleteMessageBoard(long id, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserEntity user = (UserEntity) session.getAttribute("user");

        MessagesBoardEntity msgEntity = messagesBoardRepository.selectOne(
                new QueryWrapper<MessagesBoardEntity>()
                        .eq("id", id)
        );
        if (ObjectUtils.isEmpty(msgEntity)) {
            return null;
        }
        if (!user.getUsername().equals(msgEntity.getCreateBy())) {     //判断是否删除的是自己的留言
            if (!"DBA".equals(user.getIdentity())) {                //是否为管理员
                return null;
            } else {
                QueryWrapper<MessagesBoardEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("id", id);
                int del = messagesBoardRepository.delete(queryWrapper);
                if (!ObjectUtils.isEmpty(del)) {
                    return "token:" + del;
                }
            }

        }
        QueryWrapper<MessagesBoardEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int del = messagesBoardRepository.delete(queryWrapper);
        if (!ObjectUtils.isEmpty(del)) {
            return "token:" + del;
        }
        return null;
    }
}
